package com.example.redis.tool;

import org.apache.curator.RetryPolicy;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.framework.recipes.locks.InterProcessMutex;
import org.apache.curator.retry.ExponentialBackoffRetry;

import java.util.concurrent.TimeUnit;

/**
 * @author:xuecx
 * @descript:<p> </p>
 * @date:2019/6/14
 */
public class ZookeeperLock {
    public static void main(String[] args) {
        //创建客户端
        RetryPolicy policy=new ExponentialBackoffRetry(1000,3);
        CuratorFramework client= CuratorFrameworkFactory.newClient("192.168.205.128:2181,192.168.205.130:2181,192.168.205.131:2181",policy);
        client.start();

        InterProcessMutex mutex=new InterProcessMutex(client,"/xuecx1/lock");
        try {
            mutex.acquire();
            System.out.println("测试处理任务");
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                mutex.release();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        client.close();
    }
}
